package com.xhkj.mapper;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.ibatis.mapping.MappedStatement;

import com.google.common.base.CaseFormat;

import tk.mybatis.mapper.annotation.LogicDelete;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.MapperTemplate;
import tk.mybatis.mapper.mapperhelper.SqlHelper;
public class MyProvider extends MapperTemplate {
	public MyProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
		super(mapperClass, mapperHelper);
	}
	/**
	 * 真实删除
	 * */
	public String deleteReal(MappedStatement ms) {
		final Class<?> entityClass = getEntityClass(ms);
		StringBuilder sql = new StringBuilder();
		sql.append(SqlHelper.deleteFromTable(entityClass, tableName(entityClass)));
		sql.append(" <where> id = #{id} </where>");
		return sql.toString();
	}
	
	/**
	 * 分页条件查询
	 * */
	public String selectMy(MappedStatement ms) {
		Class<?> entityClass = getEntityClass(ms);
		setResultType(ms, entityClass);
		StringBuilder sql = new StringBuilder();
		sql.append(SqlHelper.selectAllColumns(entityClass));
		sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
		sql.append(whereAllIfColumns(entityClass));
		sql.append(SqlHelper.orderByDefault(entityClass));
		return sql.toString();
	}

	/**
	 * where条件拼接
	 */
	public String whereAllIfColumns(Class<?> parameterType) {
		StringBuilder sb = new StringBuilder();
		String tmp = "";
		sb.append("<where>");
		
		Field[] fields = getAllFields(parameterType);
		for (Field field : fields) {
			// 不查询逻辑删除的数据
			if (field.isAnnotationPresent(LogicDelete.class)) {
				LogicDelete ld = field.getAnnotation(LogicDelete.class);
				tmp = " AND _column=_field";
				sb.append(tmp.replaceAll("_field", String.valueOf(ld.notDeletedValue())).replaceAll("_column",
						CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, field.getName())));
			}
			// 标记为查询条件的字段在执行下边代码
			if (field.isAnnotationPresent(QueryParam.class)) {
				QueryParam qp = field.getAnnotation(QueryParam.class);
				String type = qp.type();
				String fieldName = field.getName();
				if (type.equals("eq")) {
					tmp = "<if test=\"_field != null\"> AND _column=#{_field}</if>";
				} else if (type.equals("like")) {
					tmp = "<if test=\"_field != null\"> AND _column like CONCAT('%',#{_field},'%') </if>";
				} else if (type.equals("gt")) {
					tmp = "<if test=\"_field != null\"> AND _column &gt;#{_field}</if>";
				} else if (type.equals("gte")) {
					tmp = "<if test=\"_field != null\"> AND _column &gt;=#{_field}</if>";
				} else if (type.equals("lt")) {
					tmp = "<if test=\"_field != null\"> AND _column &lt;#{_field}</if>";
				} else if (type.equals("lte")) {
					tmp = "<if test=\"_field != null\"> AND _column &lt;=#{_field}</if>";
				} else if (type.contains("=")) {
					String[] d = type.split("=");
					if (d.length == 2) {
						fieldName = d[1];
						if (d[0].equals("gt")) {
							tmp = "<if test=\"_field != null\"> AND _column &gt;#{_field}</if>";
						}
						if (d[0].equals("gte")) {
							tmp = "<if test=\"_field != null\"> AND _column &gt;=#{_field}</if>";
						}
						if (d[0].equals("lt")) {
							tmp = "<if test=\"_field != null\"> AND _column &lt;#{_field}</if>";
						}
						if (d[0].equals("lte")) {
							tmp = "<if test=\"_field != null\"> AND _column &lt;=#{_field}</if>";
						}
					}

				}

				sb.append(tmp.replaceAll("_field", field.getName()).replaceAll("_column",
						CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, fieldName)));
			}

		}

		sb.append("</where>");
		return sb.toString();
	}
	/**
	 * 获取类的所有属性(包括父类)
	 * */
	public static Field[] getAllFields(Class<?> clazz) {
		List<Field> fieldList = new ArrayList<>();
		while (clazz != null) {
			fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));
			clazz = clazz.getSuperclass();
		}
		Field[] fields = new Field[fieldList.size()];
		fieldList.toArray(fields);
		return fields;
	}
}
